package com.googlecode.economini;

import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ComponentName;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import com.googlecode.economini.issues.IssueManager;
import com.googlecode.economini.issues.IssueProvider;
import com.googlecode.economini.issues.IssueService;
import com.googlecode.economini.issues.IssueStore;

public class IssueRack extends ListActivity implements LoaderCallbacks<Cursor> {

  private IssueManager issueManager;
  private CursorAdapter issueListAdapter;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.issue_selection_view);

    getLoaderManager().initLoader(0, null, this);

    issueListAdapter = new SimpleCursorAdapter(
        this,
        android.R.layout.simple_list_item_1,
        null,
        new String[] { IssueStore.TITLE_COLUMN },
        new int[] { android.R.id.text1 },
        0);
    setListAdapter(issueListAdapter);

    Intent issueServiceIntent = new Intent(this, IssueService.class);
    bindService(
        issueServiceIntent,
        new IssueServiceConnection(),
        Context.BIND_AUTO_CREATE);
  }

  private class IssueServiceConnection implements ServiceConnection {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      // TODO which thread does this occur on?
      issueManager = (IssueManager) service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
      issueManager = null;
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.issue_selection_menu, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.refresh_item_list:
        if (issueManager != null) {
          issueManager.refreshIssues();
        }
        return true;
      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    issueManager.loadIssue(id);
  }

  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    CursorLoader loader = new CursorLoader(
        this,
        IssueProvider.ISSUE_URI,
        new String[] { IssueStore.ID_COLUMN, IssueStore.TITLE_COLUMN },
        null,
        null,
        IssueStore.TITLE_COLUMN + " DESC");

    return loader;
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    issueListAdapter.swapCursor(cursor);
  }

  @Override
  public void onLoaderReset(Loader<Cursor> loader) {
    // Cursor should be closed by the caller
    issueListAdapter.swapCursor(null);
  }
}